package com.anita.member.adapter.in.web.controller;

import com.anita.member.adapter.in.web.dto.*;
import com.anita.member.application.command.*;
import com.anita.member.application.dto.MemberLoginResponse;
import com.anita.member.application.dto.MemberProfileResponse;
import com.anita.member.application.dto.MemberRegisterResponse;
import com.anita.member.application.port.in.*;
import com.anita.member.common.Result;
import com.anita.member.common.UploadResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 会员管理
 * @author qiu
 * @date 2025/10/17
 * @description
 */
@Slf4j
@Tag(name = "会员管理", description = "会员注册、登录")
@RestController
@RequestMapping("/api/member")
public class MemberController {

    @Resource
    private MemberRegisterUseCase memberRegisterUseCase;

    @Resource
    private MemberLoginUseCase memberLoginUseCase;

    @Resource
    private MemberProfileUseCase memberProfileUseCase;

    @Resource
    private MemberUpdateProfileUseCase memberUpdateProfileUseCase;

    @Resource
    private MemberUpdatePasswordUseCase memberUpdatePasswordUseCase;

    @Resource
    private MemberUpdateAvatarUseCase memberUpdateAvatarUseCase;

    @PostMapping("/register")
    @Operation(summary = "会员注册", description = "新会员注册")
    public Result<MemberRegisterResponse> register(@Valid @RequestBody MemberRegisterRequest request){
        MemberRegisterCommand command = new MemberRegisterCommand(
                request.username(),
                request.nickname(),
                request.password(),
                request.phone(),
                request.email(),
                request.avatar()
        );
        MemberRegisterResponse response = memberRegisterUseCase.registerMember(command);
        return Result.success(response);
    }

    @PostMapping("/login")
    @Operation(summary = "会员登录", description = "会员通过用户名和密码登录系统")
    public Result<MemberLoginResponse> login(@Valid @RequestBody MemberLoginRequest request, HttpServletRequest servletRequest){
        MemberLoginCommand command = new MemberLoginCommand(
                request.username(),
                request.password(),
                servletRequest
        );
        MemberLoginResponse response = memberLoginUseCase.login(command);
        return Result.success(response);
    }

    @GetMapping("/profile/{id}")
    @Operation(summary = "获取会员资料", description = "根据id获取会员详细资料")
    public Result<MemberProfileResponse> getProfile(
            @Parameter(description = "会员id", required = true)
            @PathVariable("id") Long id) {
        MemberProfileResponse response = memberProfileUseCase.getProfile(id);
        return Result.success(response);

    }

    @PutMapping("/profile/{id}")
    @Operation(summary = "更新会员资料", description = "根据id更新会员详细资料")
    public Result<String> updateProfile(
            @Parameter(description = "会员id", required = true)
            @PathVariable("id") Long id,
            @Valid @RequestBody MemberUpdateProfileRequest request) {
        MemberUpdateProfileCommand command = new MemberUpdateProfileCommand(
                id,
                request.username(),
                request.nickname(),
                request.avatar(),
                request.email(),
                request.phone(),
                request.gender(),
                request.birthday(),
                request.location(),
                request.bio()
        );
        memberUpdateProfileUseCase.updateProfile(command);
        return Result.success("会员资料更新成功");
    }

    @PutMapping("/password/{id}")
    @Operation(summary = "修改会员密码", description = "根据会员id修改登录密码")
    public Result<String> updatePassword(
            @Parameter(description = "会员id", required = true)
            @PathVariable("id") Long id,
            @Valid @RequestBody MemberUpdatePasswordRequest request) {
        MemberUpdatePasswordCommand command = new MemberUpdatePasswordCommand(
                id,
                request.oldPassword(),
                request.newPassword()
        );
        memberUpdatePasswordUseCase.updatePassword(command);
        return Result.success("会员密码更新成功");
    }

    @PutMapping("/avatar/{id}")
    @Operation(summary = "修改会员头像", description = "根据会员id修改头像")
    public Result<String> updateAvatar(
            @Parameter(description = "会员id", required = true)
            @PathVariable("id") Long id,
            @NotNull @RequestParam("file") MultipartFile file) {

        String url = memberUpdateAvatarUseCase.updateAvatar(id, file);
        return Result.success("会员头像更新成功, url: " + url);
    }
}
